package wfmanage.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.MyLog;

import edu.wfmanage.models.Claim;
import edu.wfmanage.models.User;

public class verificationResult extends ActionSupport{
	/**
	Author: Satya Deepthi Bhagi
	**/
	private static final long serialVersionUID = 1L;
	private String verificationResult;
	private int RequestId;
	private String verificationComments;
	private String Submit;
	private String username;
	private List<Claim> verifyitems;
	private List<User> userDetails;
	String toList="";
	public String execute() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		User user = (User) session.get("user");
		if (user == null) {
			user = new User();
		}
		username=user.getUsername();
		if (this.verificationComments.isEmpty()) {
		    return "initial_entry";
        } 
		if (Submit.equalsIgnoreCase("Back")) {
			MyLog.log("In back");
			verifyitems=new ArrayList<Claim>();
			verifyitems = Claim.findverifyitems("where VERIFY_DPT_STATUS='verfication pending'  and status='pending' ");
			return "back";
		}
		if (Submit.equalsIgnoreCase("Finish")){
			String updatesql=null;
			MyLog.log("In updating db");
			if(verificationResult.equals("VerificationPassed")){
		 updatesql="update request set VERIFY_DPT_STATUS='"+ getVerificationResult()+"', VERIFY_DPT_ACTION_TIME=sysdate(),VERIFY_DPT_COMMENT='"+
				getVerificationComments()+"',VERIFIED_BY='"+user.getUsername() +"' where req_id="+getRequestId();
		 Claim.updatedb(updatesql);
		 verifyitems=new ArrayList<Claim>();
			verifyitems = Claim.findverifyitems("where VERIFY_DPT_STATUS='verfication pending'  and status='pending' ");
			userDetails=User.userDetails("where emp_role ='finance_dpt'");
			email mail=new email();
			for(User u: userDetails){
				MyLog.log("user is"+u.getEmailid());
		//toList+="\""+u.getEmailid()+"\", ";
				mail.sendmail(u.getUsername(),u.getEmailid(),"finance_dpt",RequestId,"NA");
				MyLog.log("An email has been sent to"+u.getEmailid());
			}			
			return "approved";
			}
			if(verificationResult.equals("VerificationFailed")){
				updatesql="update request set VERIFY_DPT_STATUS='"+ getVerificationResult()+"', VERIFY_DPT_ACTION_TIME=sysdate(),VERIFY_DPT_COMMENT='"+
						getVerificationComments()+"',VERIFIED_BY='"+user.getUsername() +"', STATUS='Rejected by VerificationDept' where req_id="+getRequestId();
				Claim.updatedb(updatesql);
				verifyitems=new ArrayList<Claim>();
				verifyitems = Claim.findverifyitems("where VERIFY_DPT_STATUS='verfication pending'  and status='pending' ");
			
					}
		     }
		
		userDetails=User.userDetails("where emp_role ='finance_dpt'");
		email mail=new email();
		for(User u: userDetails){
			MyLog.log("user is"+u.getEmailid());
	//toList+="\""+u.getEmailid()+"\", ";
			mail.sendmail(u.getUsername(),u.getEmailid(),"finance_dpt",RequestId,"NA");
			MyLog.log("An email has been sent to"+u.getEmailid());
		}			
		 		return "rejected";
	}

	
	public String getVerificationResult() {
		return verificationResult;
	}


	public void setVerificationResult(String verificationResult) {
		this.verificationResult = verificationResult;
	}


	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getSubmit() {
		return Submit;
	}
	public void setSubmit(String submit) {
		Submit = submit;
	}

	public int getRequestId() {
		return RequestId;
	}

	public void setRequestId(int requestId) {
		RequestId = requestId;
	}

	public String getVerificationComments() {
		return verificationComments;
	}

	public void setVerificationComments(String verificationComments) {
		this.verificationComments = verificationComments;
	}


	public List<Claim> getVerifyitems() {
		return verifyitems;
	}


	public void setVerifyitems(List<Claim> verifyitems) {
		this.verifyitems = verifyitems;
	}
	
	
}
